package com.xydtech.ecard.custgroup.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.GeoUtil;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.common.utils.sql.SqlUtil;
import com.xydtech.ecard.config.service.IEcardApiSysConfigService;
import com.xydtech.ecard.custgroup.domain.*;
import com.xydtech.ecard.custgroup.service.*;
import com.xydtech.ecard.ecard.controller.EcardApiBaseController;
import com.xydtech.ecard.ecard.domain.*;
import com.xydtech.ecard.ecard.service.IEcardApiMerchantInfoService;
import com.xydtech.ecard.ecard.service.IEcardApiMerchantVisitInfoService;
import com.xydtech.ecard.user.domain.EcardApiEcardRegistInfo;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiEcardRegistInfoService;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.domain.SysConfig;
import com.xydtech.system.domain.SysWechatApplet;
import com.xydtech.system.service.ISysConfigService;
import com.xydtech.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 电子名片-行业客群营销接口类
 *
 * @author yuzhiguo
 * @date 2024-11-27 下午3:45
 */
@Controller
@Slf4j
@Api(tags = "电子名片-企业营销接口")
@RequestMapping("/ecardSaasApi/customerGroupMarketing")
public class EcardApiCustomerGroupInfoController extends EcardApiBaseController {

    //4:客户经理
    private static String roles = "4";
    //客户经理岗位，后台没维护，这里用不到
    private static String cm = "cm";
    //网格管理员岗位
    private static String wg = "wg";

    @Autowired
    private IEcardApiUserService ecardApiUserService;
    @Autowired
    private IEcardApiCustomerGroupInfoService ecardCustomerGroupInfoService;
    @Autowired
    private IEcardApiCustomerGroupMerchantService ecardCustomerGroupMerchantService;
    @Autowired
    private IEcardApiEcardRegistInfoService registInfoService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IEcardApiMerchantInfoService ecardMerchantInfoService;
    @Autowired
    private IEcardApiMerchantVisitInfoService ecardMerchantVisitInfoService;
    @Autowired
    private IEcardApiCustomerGroupOrganizeService ecardCustomerGroupOrganizeService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IEcardApiSysConfigService ecardApiSysConfigService;
    @Autowired
    private IEcardApiCustomerOrganizeVisitService ecardCustomerOrganizeVisitService;
    @Autowired
    private IEcardApiCustomerOrganizeTaskService ecardCustomerOrganizeTaskService;


    /**
     * 电子名片-条件查询登录人法人机构下客群列表接口
     *
     * @author yuzhiguo
     * @date 2024/11/27 下午3:48
     */
    @PostMapping(value = "/getCustomerGroupList")
    @ApiOperation(value = "电子名片-条件查询登录人法人机构下客群列表接口", notes = "电子名片-条件查询登录人法人机构下客群列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getCustomerGroupList(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());

            JSONObject jsonObject = JSONObject.parseObject(json);
            String customerGroupType = jsonObject.getString("customerGroupType");//客群类型，1：行业客群
            Long id = jsonObject.getLong("id");//客群名称id（筛选条件）
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");

            if (StringUtils.objIsNullOrEmpty(customerGroupType)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }

            EcardApiCustomerGroupInfo ecardCustomerGroupInfo = new EcardApiCustomerGroupInfo();
            ecardCustomerGroupInfo.setId(id);
            ecardCustomerGroupInfo.setDeptId(wechatApplet.getDeptId());
            ecardCustomerGroupInfo.setDictDeptId(wechatApplet.getDeptId());
            PageHelper.startPage(pageNum, pageSize);
            List<EcardApiCustomerGroupInfo> customerGroupList = ecardCustomerGroupInfoService.getCustomerGroupList(ecardCustomerGroupInfo);
            List<EcardApiCustomerGroupInfo> apiCustomerGroupList = new ArrayList<>();
            PageInfo pageInfo = new PageInfo(customerGroupList);

            for (EcardApiCustomerGroupInfo customerGroupInfo : customerGroupList) {
                //查询客群中的客户数量
                EcardApiCustomerGroupMerchant customerGroupMerchant = new EcardApiCustomerGroupMerchant();
                customerGroupMerchant.setCustomerId(customerGroupInfo.getId());//客群id
                customerGroupMerchant.setState(1);//状态为启用
                List<EcardApiCustomerGroupMerchant> customerGroupMerchantList = ecardCustomerGroupMerchantService.selectEcardCustomerGroupMerchantList(customerGroupMerchant);
                customerGroupInfo.setCustNumber(customerGroupMerchantList.size() + "");
                apiCustomerGroupList.add(customerGroupInfo);
            }
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), apiCustomerGroupList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-查询登录人法人机构下客群信息
     *
     * @author yuzhiguo
     * @date 2024/11/27 下午3:48
     */
    @PostMapping(value = "/getCustomerGroupBySearch")
    @ApiOperation(value = "电子名片-查询登录人法人机构下客群信息", notes = "电子名片-查询登录人法人机构下客群信息")
    @ResponseBody
    @RsaRequest
    public ResponseModel getCustomerGroupBySearch(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());

            JSONObject jsonObject = JSONObject.parseObject(json);
            String customerGroupType = jsonObject.getString("customerGroupType");//客群类型，1：行业客群

            if (StringUtils.objIsNullOrEmpty(customerGroupType)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardApiCustomerGroupInfo ecardCustomerGroupInfo = new EcardApiCustomerGroupInfo();
            ecardCustomerGroupInfo.setDeptId(wechatApplet.getDeptId());
            ecardCustomerGroupInfo.setCustomerGroupType(customerGroupType);
            ecardCustomerGroupInfo.setDictDeptId(wechatApplet.getDeptId());
            List<EcardApiCustomerGroupInfo> customerGroupList = ecardCustomerGroupInfoService.getCustomerGroupList(ecardCustomerGroupInfo);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(customerGroupList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-条件查询登录人客群下的行业客户接口
     *
     * @author yuzhiguo
     * @date 2024/11/28 上午11:29
     */
    @PostMapping(value = "/getIndustrialCustomerList")
    @ApiOperation(value = "电子名片-条件查询登录人客群下的行业客户接口", notes = "电子名片-条件查询登录人客群下的行业客户接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getIndustrialCustomerList(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());

            JSONObject jsonObject = JSONObject.parseObject(json);
            Long id = jsonObject.getLong("id");//客群名称id
            String licenseName = jsonObject.getString("licenseName");//客户名称
            String isOrganize = jsonObject.getString("isOrganize");//是否归属社团组织 字典 common_yes_no，T:是，F：否
            Long organizeId = jsonObject.getLong("organizeId");//社团组织id，用于在社团组织列表中点击展示的行业客户列表
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");

            if (StringUtils.objIsNullOrEmpty(id)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }

            EcardApiCustomerGroupInfo customerGroupInfo = ecardCustomerGroupInfoService.selectEcardCustomerGroupInfoById(id);
            EcardApiCustomerGroupMerchant ecardCustomerGroupMerchant = new EcardApiCustomerGroupMerchant();
            ecardCustomerGroupMerchant.setId(id);
            ecardCustomerGroupMerchant.setManagementPersonnelId(Long.valueOf(currentUserId));
            ecardCustomerGroupMerchant.setCustomerGroupType(customerGroupInfo.getCustomerGroupType());//客群类型，1：行业客群
            ecardCustomerGroupMerchant.setDictDeptId(wechatApplet.getDeptId());//字典查询条件
            ecardCustomerGroupMerchant.setLicenseName(licenseName);//客户名称查询条件
            ecardCustomerGroupMerchant.setIsOrganize(isOrganize);//是否归属社团组织查询条件，T：是，F：否
            ecardCustomerGroupMerchant.setOrganizeId(organizeId);//社团组织id

            PageHelper.startPage(pageNum, pageSize);
            List<EcardApiCustomerGroupMerchant> customerGroupMerchantList = ecardCustomerGroupMerchantService.getIndustrialCustomerList(ecardCustomerGroupMerchant, salt);
            List<EcardApiCustomerGroupMerchant> apiCustomerGroupGroupMerchantList = new ArrayList<>();
            PageInfo pageInfo = new PageInfo(customerGroupMerchantList);

            for (EcardApiCustomerGroupMerchant groupMerchant : customerGroupMerchantList) {
                //查询总拜访次数
                EcardApiMerchantVisitInfo merchantVisitInfo = new EcardApiMerchantVisitInfo();
                merchantVisitInfo.setMerchantId(groupMerchant.getMerchantId());
                merchantVisitInfo.setMerchantType(groupMerchant.getMerchantType());//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
                List<EcardApiMerchantVisitInfo> visitInfoList = ecardMerchantVisitInfoService.getMerchantVisitList(merchantVisitInfo);
                groupMerchant.setVisitNumber(visitInfoList.size() + "");

                //查询最近一次拜访时间
                String lastVisitTime = ecardMerchantVisitInfoService.getMerchantLastVisitTime(groupMerchant.getMerchantId(), groupMerchant.getMerchantType());//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
                if (StringUtils.isNotEmpty(lastVisitTime)) {
                    groupMerchant.setLastVisitDate(DateUtils.parseDate(lastVisitTime));
                }
                apiCustomerGroupGroupMerchantList.add(groupMerchant);
            }
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), apiCustomerGroupGroupMerchantList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-行业客户转交接口
     *
     * @author yuzhiguo
     * @date 2024/11/28 下午2:39
     */
    @PostMapping(value = "/addIndustrialCustomerTransferInfo")
    @ApiOperation(value = "电子名片-行业客户转交接口", notes = "电子名片-行业客户转交接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel addIndustrialCustomerTransferInfo(@RequestBody EcardApiMerchantHandover ecardMerchantHandover) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前登录人userId
            ecardMerchantHandover.setTransferId(Long.valueOf(currentUserId));
            ecardMerchantHandover.setTransferDeptId(userInfo.getDeptId());
            //转交对应商户信息，方法里面同步转交行业客户关联表信息
            int updateCount = ecardMerchantInfoService.addMerchantTransferInfo(ecardMerchantHandover);
            return updateCount > 0 ? ResponseModel.ok() : ResponseModel.error("转交失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-根据ID修改行业客户状态（删除按钮）
     *
     * @author yuzhiguo
     * @date 2024/11/28 下午3:07
     */
    @PostMapping(value = "/changeIndustrialCustomerStatus")
    @ApiOperation(value = "电子名片-根据ID修改行业客户状态", notes = "电子名片-根据ID修改行业客户状态")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel changeIndustrialCustomerStatus(@RequestBody String json) {
        try {
            /**
             * 先根据id修改行业客户关联表中的状态
             * 再根据商户id和商户类型去修改对应商户信息表中的状态
             */
            JSONObject jsonObject = JSONObject.parseObject(json);
            Long id = jsonObject.getLong("id");//行业客户id
            if (StringUtils.objIsNullOrEmpty(id)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            //修改行业客户关联表中的状态，方法中同步修改对应商户信息的状态
            int updateCount = ecardCustomerGroupMerchantService.changeIndustrialCustomerStatus(id);
            return updateCount > 0 ? ResponseModel.ok() : ResponseModel.error("删除失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-分词查询法人机构下商户/企业/新型农业主体工商信息列表接口
     *
     * @author yuzhiguo
     * @date 2024/11/28 上午9:57
     */
    @PostMapping(value = "/getIndustrialCommercialList")
    @ApiOperation(value = "电子名片-分词查询法人机构下商户/企业/新型农业主体工商信息列表接口", notes = "电子名片-分词查询法人机构下商户/企业/新型农业主体工商信息列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getIndustrialCommercialList(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());

            JSONObject jsonObject = JSONObject.parseObject(json);
            String merchantType = jsonObject.getString("merchantType");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
            String licenseName = jsonObject.getString("licenseName");//商户名称
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");
            if (StringUtils.objIsNullOrEmpty(merchantType) || StringUtils.objIsNullOrEmpty(licenseName)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            PageHelper.startPage(pageNum, pageSize);
            //分词查询法人机构下的工商信息，去除已经是行业客户的数据
            List<EcardApiMerchantInfo> merchantInfoList = ecardMerchantInfoService.selectParticipleEcardMerchantInfo(licenseName,
                    merchantType,
                    ecardApiEcardRegistInfo.getRegistId(),
                    wechatApplet.getDeptId(),
                    salt);
            PageInfo pageInfo = new PageInfo(merchantInfoList);

            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), merchantInfoList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-保存行业客户数据接口,只有客户经理能保存行业客户信息
     *
     * @author yuzhiguo
     * @date 2024/11/28 上午8:39
     */
    @PostMapping(value = "/addCustomerGroupMerchantInfo")
    @ApiOperation(value = "电子名片-保存行业客户数据接口", notes = "电子名片-保存行业客户数据接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel addCustomerGroupMerchantInfo(@RequestBody EcardApiCustomerGroupMerchant customerGroupMerchant) {
        try {
            /**
             * 思路：
             * 1、首先根据商户名称查询对应类型中有没有已建档的信息
             * 有：直接选中插入到关联表
             * 关联表传值：客群类型、客群名称id、是否归属社团组织、社团组织id、商户类型、商户id
             * 2、没有：调用之前新增商户的接口然后建档，建档完返回到新增行业客户页面再调用此接口
             */
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            boolean flag = false;//判断是新增还是编辑，true：新增，false：编辑
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            if (!roles.equals(ecardApiEcardRegistInfo.getRoles())) {
                return ResponseModel.error("您没有编辑行业客户信息的权限！");
            }
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());

            //当前登录人ID
            customerGroupMerchant.setCreatorId(Long.valueOf(currentUserId));
            //管户人id
            customerGroupMerchant.setManagementPersonnelId(Long.valueOf(currentUserId));
            //登录人机构ID
            customerGroupMerchant.setDeptId(userInfo.getDeptId());
            //法人机构id
            customerGroupMerchant.setCorpId(wechatApplet.getDeptId());

            if (!StringUtils.isNotNull(customerGroupMerchant.getId())) {
                flag = true;
                //校验选择的工商信息是否已经是行业客户
                EcardApiCustomerGroupMerchant groupMerchant = new EcardApiCustomerGroupMerchant();
                groupMerchant.setMerchantId(customerGroupMerchant.getMerchantId());
                groupMerchant.setMerchantType(customerGroupMerchant.getMerchantType());
                List<EcardApiCustomerGroupMerchant> groupMerchants = ecardCustomerGroupMerchantService.selectEcardCustomerGroupMerchantList(groupMerchant);
                if (groupMerchants.size() > 0) {
                    ResponseModel.error("该客户信息已经是行业客户！");
                }
            } else {
                //校验是否是自己的行业客户
                EcardApiCustomerGroupMerchant ecardApiCustomerGroupMerchant = ecardCustomerGroupMerchantService.selectEcardCustomerGroupMerchantById(customerGroupMerchant.getId());
                if (ecardApiCustomerGroupMerchant != null) {
                    if (ecardApiCustomerGroupMerchant.getManagementPersonnelId().longValue() != ecardApiEcardRegistInfo.getRegistId().longValue()) {
                        ResponseModel.error("您没有编辑该行业客户信息的权限！");
                    }
                } else {
                    return ResponseModel.error("操作失败，未查询到该行业客户信息！");
                }
                flag = false;
            }
            int id = ecardCustomerGroupMerchantService.addCustomerGroupMerchantInfo(customerGroupMerchant);
            return id > 0 ? ResponseModel.ok(customerGroupMerchant.getId()) : ResponseModel.error("添加失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-查询行业客户地图坐标列表接口
     *
     * @param: type 1登录人所在机构下全部，2法人机构下全部（针对网格管理岗位）
     * @author yuzhiguo
     * @date 2024/11/29 下午4:28
     */
    @PostMapping(value = "/getGroupMerchantCoordinateList")
    @ApiOperation(value = "电子名片-查询行业客户地图坐标列表接口", notes = "电子名片-查询行业客户地图坐标列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getGroupMerchantCoordinateList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            JSONObject jsonObject = JSONObject.parseObject(json);
            String type = jsonObject.getString("type");
            String coordinate = jsonObject.getString("coordinate");

            SysConfig config = new SysConfig();
            config.setConfigKey("ecard_merchant_show_distance");
            SysConfig sysConfig;

            if (StringUtils.objIsNullOrEmpty(currentUserId) || StringUtils.objIsNullOrEmpty(type)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            List<EcardApiCustomerGroupMerchant> customerGroupMerchantList;
            if ("1".equals(type)) {
                //获取当前用户的信息
                EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
                customerGroupMerchantList = ecardCustomerGroupMerchantService.getGroupMerchantCoordinateList(Long.valueOf(currentUserId), userInfo.getDeptId());
                SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
                config.setDeptId(sysDept.getDeptId());
                sysConfig = ecardApiSysConfigService.selectConfig(config);
            } else {
                EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
                //当前小程序id
                SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
                customerGroupMerchantList = ecardCustomerGroupMerchantService.getAllGroupMerchantCoordinateList(Long.valueOf(currentUserId), wechatApplet.getDeptId());
                SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(wechatApplet.getDeptId());
                config.setDeptId(sysDept.getDeptId());
                sysConfig = ecardApiSysConfigService.selectConfig(config);
            }

            //有经纬度就计算，没有取全部
            if (coordinate != null) {
                List<EcardApiCustomerGroupMerchant> list = new ArrayList<>();
                //计算是否在配置的范围之内
                //定位的行业客户经纬度
                String place[] = coordinate.split(",");
                for (EcardApiCustomerGroupMerchant ecardApiCustomerGroupMerchant : customerGroupMerchantList) {
                    String clockInPlace[] = ecardApiCustomerGroupMerchant.getCoordinate().split(",");
                    Double distance = GeoUtil.GetDistance(Double.valueOf(clockInPlace[1]), Double.valueOf(clockInPlace[0]), Double.valueOf(place[1]), Double.valueOf(place[0]));

                    BigDecimal bigDistance = new BigDecimal(distance);
                    BigDecimal bigValue = new BigDecimal(sysConfig.getConfigValue());
                    if (bigDistance.compareTo(bigValue) <= 0) {
                        list.add(ecardApiCustomerGroupMerchant);
                    }
                }
                return ResponseModel.okToJSONArray(list);
            } else {
                return ResponseModel.okToJSONArray(customerGroupMerchantList);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }
    /**
     * 电子名片-条件查询登录人客群下的社团组织列表接口
     *
     * @author yuzhiguo
     * @date 2024/11/28 下午5:14
     */
    @PostMapping(value = "/getCustomerGroupOrganizeList")
    @ApiOperation(value = "电子名片-条件查询登录人客群下的社团组织列表接口", notes = "电子名片-条件查询登录人客群下的行业客户接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getCustomerGroupOrganizeList(@RequestBody String json) {
        /**
         * 1、筛选条件下的社团组织列表查询法人机构下所有的社团组织信息，根据是否本人字段区别展示按钮
         * 是：展示拜访，转交，删除按钮
         * 否：展示查看按钮
         * 2、行业客户新建时选择社团组织
         * 根据是否共享展示
         * 是：展示法人机构下所有的社团组织信息
         * 否：只展示本人名下的社团组织信息
         */
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            //查询法人机构下的所有社团组织
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            JSONObject jsonObject = JSONObject.parseObject(json);
            Long id = jsonObject.getLong("id");//客群名称id
            String type = jsonObject.getString("type");//类型 1：查询行业客户建档时选择的社团组织列表，2：查询行业客户列表筛选条件选择社团组织时展示的列表
            String organizeName = jsonObject.getString("organizeName");//社团组织名称（筛选条件）
            String organizeType = jsonObject.getString("organizeType");//社团组织类型，字典：ecard_organize_type
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");

            if (StringUtils.objIsNullOrEmpty(id) || StringUtils.objIsNullOrEmpty(type)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardApiCustomerGroupInfo customerGroupInfo = ecardCustomerGroupInfoService.selectEcardCustomerGroupInfoById(id);//客群信息
            List<EcardApiCustomerGroupOrganize> customerGroupOrganizeList = new ArrayList<>();
            PageHelper.startPage(pageNum, pageSize);
            EcardApiCustomerGroupOrganize ecardApiCustomerGroupOrganize = new EcardApiCustomerGroupOrganize();
            ecardApiCustomerGroupOrganize.setOrganizeName(organizeName);//社团组织名称

            //查询行业客户列表筛选条件选择社团组织时展示的列表，展示法人机构下所有
            if ("2".equals(type)) {
                ecardApiCustomerGroupOrganize.setDeptId(sysDept.getDeptId());
                customerGroupOrganizeList = ecardCustomerGroupOrganizeService.getOrganizeInfoList(ecardApiCustomerGroupOrganize, salt);
                //处理每一次条社团组织信息中的行业客户数量
                for (EcardApiCustomerGroupOrganize groupOrganize : customerGroupOrganizeList) {
                    //查询社团组织下的行业客户数量
                    /*EcardApiCustomerGroupMerchant customerGroupMerchant = new EcardApiCustomerGroupMerchant();
                    customerGroupMerchant.setCustomerGroupType(customerGroupInfo.getCustomerGroupType());//客群类型
                    customerGroupMerchant.setOrganizeId(groupOrganize.getId());
                    customerGroupMerchant.setManagementPersonnelId(Long.valueOf(currentUserId));
                    customerGroupMerchant.setDictDeptId(wechatApplet.getDeptId());
                    List<EcardApiCustomerGroupMerchant> customerGroupMerchantList = ecardCustomerGroupMerchantService.getIndustrialCustomerList(customerGroupMerchant);
                    groupOrganize.setCustNumber(customerGroupMerchantList.size() + "");*/

                    //查询总拜访次数
                    EcardApiCustomerOrganizeVisit organizeVisit = new EcardApiCustomerOrganizeVisit();
                    organizeVisit.setOrganizeId(groupOrganize.getId());
                    organizeVisit.setState(1);//启用
                    List<EcardApiCustomerOrganizeVisit> visitInfoList = ecardCustomerOrganizeVisitService.getOrganizeVisitList(organizeVisit);
                    groupOrganize.setVisitNumber(visitInfoList.size() + "");

                    //查询最近一次拜访时间
                    String lastVisitTime = ecardCustomerOrganizeVisitService.getOrganizeLastVisitTime(groupOrganize.getId());
                    if (StringUtils.isNotEmpty(lastVisitTime)) {
                        groupOrganize.setLastVisitDate(DateUtils.parseDate(lastVisitTime));
                    }

                    //处理是否删除字段，用于小程序列表是否删除校验，0：是，1：否
                    EcardApiCustomerGroupMerchant groupMerchant = new EcardApiCustomerGroupMerchant();
                    groupMerchant.setOrganizeId(groupOrganize.getId());
                    groupMerchant.setCustomerGroupType(customerGroupInfo.getCustomerGroupType());//客群类型，1：行业客群
                    groupMerchant.setDictDeptId(wechatApplet.getDeptId());
                    List<EcardApiCustomerGroupMerchant> groupMerchants = ecardCustomerGroupMerchantService.getIndustrialCustomerList(groupMerchant, salt);
                    if (groupMerchants.size() > 0) {
                        groupOrganize.setIsDelete("0"); //是否删除校验，0是，1否
                    } else {
                        groupOrganize.setIsDelete("1"); //是否删除校验，0是，1否
                    }

                    //处理是否本人字段，用于小程序列表是否显示编辑按钮，0：是，1：否
                    //校验该社团组织是否在本人名下
                    if (groupOrganize.getManagementPersonnelId().longValue() != ecardApiEcardRegistInfo.getRegistId().longValue()) {
                        groupOrganize.setIsOneself("1");//否 不显示编辑按钮
                    } else {
                        groupOrganize.setIsOneself("0");//是 显示编辑按钮
                    }
                }
            }

            //行业客户建档时选择社团组织，社团组织信息列表根据参数和社团类型展示
            if ("1".equals(type)) {
                //电子名片行业客户信息是否共享社团组织 0：是，1：否
                String configValue = configService.selectConfigByKey("ecard_group_organize_share", sysDept.getDeptId());
                if ("0".equals(configValue)) {
                    //是：查询法人机构下下所有的社团组织列表
                    ecardApiCustomerGroupOrganize.setDeptId(sysDept.getDeptId());
                    ecardApiCustomerGroupOrganize.setOrganizeType(organizeType);
                    customerGroupOrganizeList = ecardCustomerGroupOrganizeService.getOrganizeInfoList(ecardApiCustomerGroupOrganize, salt);
                } else {
                    //否：只查本人名下的社团组织列表
                    ecardApiCustomerGroupOrganize.setManagementPersonnelId(Long.valueOf(currentUserId));
                    ecardApiCustomerGroupOrganize.setDeptId(userInfo.getDeptId());
                    ecardApiCustomerGroupOrganize.setOrganizeType(organizeType);
                    customerGroupOrganizeList = ecardCustomerGroupOrganizeService.getOrganizeInfoList(ecardApiCustomerGroupOrganize, salt);
                }
            }
            PageInfo pageInfo = new PageInfo(customerGroupOrganizeList);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), customerGroupOrganizeList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-查询社团组织详情接口
     *
     * @author yuzhiguo
     * @date 2024/11/29 上午10:52
     */
    @PostMapping("/getCustomerGroupOrganizeInfo")
    @ApiOperation(value = "电子名片-查询社团组织详情接口", notes = "电子名片-查询社团组织详情接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getCustomerGroupOrganizeInfo(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            if (!roles.equals(ecardApiEcardRegistInfo.getRoles())) {
                return ResponseModel.error("您没有查看该社团组织的权限！");
            }
            JSONObject jsonObject = JSONObject.parseObject(json);
            Long id = jsonObject.getLong("id");//社团组织id
            if (StringUtils.objIsNullOrEmpty(id)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardApiCustomerGroupOrganize groupOrganize = ecardCustomerGroupOrganizeService.selecteEardCustomerGroupOrganizeById(id, salt);
            return ResponseModel.okToJSON(groupOrganize);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-保存社团组织数据接口
     *
     * @author yuzhiguo
     * @date 2024/11/29 下午12:58
     */
    @PostMapping(value = "/addCustomerGroupOrganize")
    @ApiOperation(value = "电子名片-保存社团组织数据接口", notes = "电子名片-保存社团组织数据接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel addCustomerGroupOrganize(@RequestBody EcardApiCustomerGroupOrganize ecardCustomerGroupOrganize) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            Long organizeId = ecardCustomerGroupOrganize.getId();//社团组织id
            boolean flag = false;//判断是新增还是编辑，true：新增，false：编辑
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            if (!roles.equals(ecardApiEcardRegistInfo.getRoles())) {
                return ResponseModel.error("您没有编辑社团组织信息的权限！");
            }
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());

            //当前登录人ID
            ecardCustomerGroupOrganize.setCreatorId(Long.valueOf(currentUserId));
            //管户人id
            ecardCustomerGroupOrganize.setManagementPersonnelId(Long.valueOf(currentUserId));
            //登录人机构ID
            ecardCustomerGroupOrganize.setDeptId(userInfo.getDeptId());
            //法人机构id
            ecardCustomerGroupOrganize.setCorpId(wechatApplet.getDeptId());
            if (!StringUtils.isNotNull(ecardCustomerGroupOrganize.getId())) {
                int count = ecardCustomerGroupOrganizeService.validateGroupOrganizeInfo(ecardCustomerGroupOrganize);
                if (count > 0) {
                    return ResponseModel.error("相同社团组织名称不能重复！");
                }
                flag = true;
            } else {
                //校验是否是自己的企业
                EcardApiCustomerGroupOrganize ecardApiCustomerGroupOrganize = ecardCustomerGroupOrganizeService.selecteEardCustomerGroupOrganizeById(ecardCustomerGroupOrganize.getId(), salt);
                if (ecardApiCustomerGroupOrganize != null) {
                    if (ecardApiCustomerGroupOrganize.getManagementPersonnelId().longValue() != ecardApiEcardRegistInfo.getRegistId().longValue()) {
                        ResponseModel.error("您没有编辑该社团组织信息的权限！");
                    }
                } else {
                    return ResponseModel.error("操作失败，未查询到该社团组织信息！");
                }
                flag = false;
            }
            int id = ecardCustomerGroupOrganizeService.addCustomerGroupOrganize(ecardCustomerGroupOrganize, salt);

            if (flag) {//只有新增社团组织时添加拜访任务
                //查询参数配置-拜访任务定时天数
                SysConfig config = new SysConfig();
                config.setConfigKey("ecard_merchant_task_days");
                SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
                config.setDeptId(sysDept.getDeptId());
                SysConfig sysConfig = ecardApiSysConfigService.selectConfig(config);
                int taskDays = Integer.valueOf(sysConfig.getConfigValue());//拜访任务定时天数
                /**
                 * 保存社团组织时新增一条拜访任务，下次回访日期默认为当前日期加拜访任务定时天数
                 */
                Date nextDay = DateUtils.getNextDay(new Date(), taskDays);
                Date endDate = DateUtils.formatDate(nextDay, DateUtils.YYYY_MM_DD);
                EcardApiCustomerOrganizeTask customerOrganizeTask = new EcardApiCustomerOrganizeTask();
                customerOrganizeTask.setOrganizeId(ecardCustomerGroupOrganize.getId());
                customerOrganizeTask.setCmRegistId(ecardCustomerGroupOrganize.getManagementPersonnelId());
                customerOrganizeTask.setDeptId(userInfo.getDeptId());
                customerOrganizeTask.setCorpId(wechatApplet.getDeptId());
                customerOrganizeTask.setEndDate(endDate);
                customerOrganizeTask.setTaskSts("0");
                ecardCustomerOrganizeTaskService.insertEcardCustomerOrganizeTask(customerOrganizeTask);
            }
            return id > 0 ? ResponseModel.ok(ecardCustomerGroupOrganize.getId()) : ResponseModel.error("添加失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 校验社团组织能否新增行业客户接口
     *
     * @author yuzhiguo
     * @date 2024/11/29 下午2:04
     */
    @PostMapping(value = "/verifyOrganizeAddInd")
    @ApiOperation(value = "电子名片-校验社团组织能否新增行业客户接口", notes = "电子名片-校验社团组织能否新增行业客户接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel verifyOrganizeAddInd(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            String configValue = configService.selectConfigByKey("ecard_group_organize_share", dept.getDeptId());
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            JSONObject jsonObject = JSONObject.parseObject(json);
            String organizeId = jsonObject.getString("organizeId");//社团组织主键id
            if (StringUtils.objIsNullOrEmpty(organizeId)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            //电子名片行业客户信息是否共享社团组织 0：是，1：否
            if (!"0".equals(configValue)) {
                //校验该社团组织是否在本人名下
                EcardApiCustomerGroupOrganize groupOrganize = ecardCustomerGroupOrganizeService.selecteEardCustomerGroupOrganizeById(Long.valueOf(organizeId), salt);
                if (groupOrganize != null) {
                    if (groupOrganize.getManagementPersonnelId().longValue() != ecardApiEcardRegistInfo.getRegistId().longValue()) {
                        return ResponseModel.error("您没有该社团组织新增行业客户的权限！");
                    }
                }
            }
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.ok();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-保存社团组织拜访记录接口
     *
     * @author yuzhiguo
     * @date 2024/11/29 下午2:24
     */
    @PostMapping(value = "/addOrganizeVisitInfo")
    @ApiOperation(value = "电子名片-保存社团组织拜访记录接口", notes = "电子名片-保存社团组织拜访记录接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel addOrganizeVisitInfo(@RequestBody EcardApiCustomerOrganizeVisit ecardCustomerOrganizeVisit) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());

            //当前登录人Id
            ecardCustomerOrganizeVisit.setCreatorId(Long.valueOf(currentUserId));
            //登录人机构ID
            ecardCustomerOrganizeVisit.setDeptId(userInfo.getDeptId());
            //法人机构id
            ecardCustomerOrganizeVisit.setCorpId(dept.getDeptId());
            int updateCount = ecardCustomerOrganizeVisitService.insertEcardCustomerOrganizeVisit(ecardCustomerOrganizeVisit);

            return updateCount > 0 ? ResponseModel.ok() : ResponseModel.error("添加失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-新增社团组织拜访校验经纬度是否在配置的范围内接口
     *
     * @author yuzhiguo
     * @date 2024/11/19 上午9:10
     */
    @PostMapping(value = "/verifyVisitInOrganize")
    @ApiOperation(value = "电子名片-新增社团组织拜访校验经纬度是否在配置的范围内接口", notes = "电子名片-新增社团组织拜访校验经纬度是否在配置的范围内接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel verifyVisitInOrganize(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            JSONObject jsonObject = JSONObject.parseObject(json);
            Long organizeId = jsonObject.getLong("organizeId");//社团组织主键id
            String coordinate = jsonObject.getString("coordinate");//拜访时的经纬度
            if (StringUtils.objIsNullOrEmpty(organizeId) || StringUtils.objIsNullOrEmpty(coordinate)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //校验拜访是否在配置的范围内
            //社团组织建档的经纬度
            EcardApiCustomerGroupOrganize customerGroupOrganize = ecardCustomerGroupOrganizeService.selecteEardCustomerGroupOrganizeById(organizeId, salt);
            String clockInPlace[] = customerGroupOrganize.getOrganizeCoordinate().split(",");//社团组织建档经纬度
            //拜访时传的经纬度
            String place[] = coordinate.split(",");
            //计算距离
            Double distance = GeoUtil.GetDistance(Double.valueOf(clockInPlace[1]), Double.valueOf(clockInPlace[0]), Double.valueOf(place[1]), Double.valueOf(place[0]));
            String tips = "社团组织";
            //查询系统参数配置的范围
            SysConfig config = new SysConfig();
            config.setConfigKey("ecard_organize_visit_distance_limit");//电子名片社团组织拜访距离限制
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            config.setDeptId(sysDept.getDeptId());
            SysConfig sysConfig = ecardApiSysConfigService.selectConfig(config);

            BigDecimal bigDistance = new BigDecimal(distance);
            BigDecimal bigValue = new BigDecimal(sysConfig.getConfigValue());
            log.info("拜访时的坐标和" + tips + "之间的距离为========================= " + bigDistance);
            if (bigDistance.compareTo(bigValue) > 0) {
                return new ResponseModel(ResultStatus.NOT_IN_THE_HOME_GRID.getCode(), "您所在位置不在该" + tips + "位置范围内！");
            } else {
                return ResponseModel.ok();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-查询社团组织拜访记录列表接口
     *
     * @author yuzhiguo
     * @date 2024/11/29 下午3:43
     */
    @PostMapping(value = "/getOrganizeVisitList")
    @ApiOperation(value = "电子名片-查询社团组织拜访记录列表接口", notes = "电子名片-查询社团组织拜访记录列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getOrganizeVisitList(@RequestBody String json) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            Long organizeId = jsonObject.getLong("organizeId");//社团组织主键id
            if (StringUtils.objIsNullOrEmpty(organizeId)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardApiCustomerOrganizeVisit customerOrganizeVisit = new EcardApiCustomerOrganizeVisit();
            customerOrganizeVisit.setOrganizeId(organizeId);
            customerOrganizeVisit.setState(1);//启用
            List<EcardApiCustomerOrganizeVisit> organizeVisitList = ecardCustomerOrganizeVisitService.getOrganizeVisitList(customerOrganizeVisit);
            return ResponseModel.okToJSONArray(organizeVisitList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-查询社团组织拜访记录详情接口（暂时不用）
     *
     * @author yuzhiguo
     * @date 2024/11/29 下午4:03
     */
    @PostMapping(value = "/getOrganizeVisitInfo")
    @ApiOperation(value = "电子名片-查询社团组织拜访记录详情接口", notes = "电子名片-查询社团组织拜访记录详情接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getOrganizeVisitInfo(@RequestBody String json) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            Long id = jsonObject.getLong("id");
            if (StringUtils.objIsNullOrEmpty(id)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardApiCustomerOrganizeVisit organizeVisit = ecardCustomerOrganizeVisitService.selectEcardCustomerOrganizeVisitById(id);
            return ResponseModel.okToJSON(organizeVisit);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-保存社团组织转交记录接口
     *
     * @author yuzhiguo
     * @date 2024/12/2 上午10:17
     */
    @PostMapping(value = "/addOrganizeTransferInfo")
    @ApiOperation(value = "电子名片-保存社团组织转交记录接口", notes = "电子名片-保存社团组织转交记录接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel addOrganizeTransferInfo(@RequestBody EcardApiCustomerOrganizeHandover ecardCustomerOrganizeHandover) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            //转交时校验该社团组织是否在本人名下
            EcardApiCustomerGroupOrganize customerGroupOrganize = ecardCustomerGroupOrganizeService.selecteEardCustomerGroupOrganizeById(ecardCustomerOrganizeHandover.getOrganizeId(), salt);
            if (customerGroupOrganize != null) {
                if (customerGroupOrganize.getManagementPersonnelId().longValue() != ecardApiEcardRegistInfo.getRegistId().longValue()) {
                    return ResponseModel.error("您没有转交该社团组织信息的权限！");
                }
            }
            //当前登录人userId
            ecardCustomerOrganizeHandover.setTransferId(Long.valueOf(currentUserId));
            ecardCustomerOrganizeHandover.setTransferDeptId(userInfo.getDeptId());
            int updateCount = ecardCustomerGroupOrganizeService.addOrganizeTransferInfo(ecardCustomerOrganizeHandover);
            return updateCount > 0 ? ResponseModel.ok() : ResponseModel.error("转交失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-根据ID修改社团组织信息状态
     *
     * @author yuzhiguo
     * @date 2024/12/2 上午10:54
     */
    @PostMapping(value = "/changeOrganizeStatus")
    @ApiOperation(value = "电子名片-根据ID修改社团组织信息状态", notes = "电子名片-根据ID修改社团组织信息状态")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel changeOrganizeStatus(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            JSONObject jsonObject = JSONObject.parseObject(json);
            Long id = jsonObject.getLong("id");
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());

            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            if (StringUtils.objIsNullOrEmpty(id)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            //删除时校验该社团组织是否在本人名下
            EcardApiCustomerGroupOrganize customerGroupOrganize = ecardCustomerGroupOrganizeService.selecteEardCustomerGroupOrganizeById(id, salt);
            if (customerGroupOrganize != null) {
                if (customerGroupOrganize.getManagementPersonnelId().longValue() != ecardApiEcardRegistInfo.getRegistId().longValue()) {
                    return ResponseModel.error("您没有删除该社团组织信息的权限！");
                }
            }
            EcardApiCustomerGroupMerchant groupMerchant = new EcardApiCustomerGroupMerchant();
            groupMerchant.setOrganizeId(id);
            groupMerchant.setDictDeptId(wechatApplet.getDeptId());
            List<EcardApiCustomerGroupMerchant> groupMerchantList = ecardCustomerGroupMerchantService.getIndustrialCustomerList(groupMerchant, salt);
            if (groupMerchantList.size() > 0) {
                return ResponseModel.error("该社团组织下存在行业客户信息，无法删除！");
            }
            int updateCount = ecardCustomerGroupOrganizeService.changeOrganizeStatus(id);
            return updateCount > 0 ? ResponseModel.ok() : ResponseModel.error("删除失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-社团组织营销任务记录接口
     *
     * @author yuzhiguo
     * @date 2024/12/2 下午1:01
     */
    @PostMapping(value = "/getOrganizeTaskList")
    @ApiOperation(value = "电子名片-社团组织营销任务记录接口", notes = "电子名片-社团组织营销任务记录接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getOrganizeTaskList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
            JSONObject jsonObject = JSONObject.parseObject(json);
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");
            String orderByStr = jsonObject.getString("orderBy");
            String search = jsonObject.getString("search");//查询条件姓名和手机号拆开
            String phone = jsonObject.getString("phone");//手机号
            String organizeType = jsonObject.getString("organizeType");//社团组织类型
            String orderBy = SqlUtil.escapeOrderBySql(orderByStr);
            PageHelper.startPage(pageNum, pageSize, orderBy);
            List<EcardApiCustomerOrganizeTask> organizeTaskList = ecardCustomerOrganizeTaskService.getOrganizeTaskList(Long.valueOf(currentUserId), search, wechatApplet.getDeptId(), phone, organizeType, salt);//字典机构查询条件
            return ResponseModel.okToJSONArray(new PageInfo(organizeTaskList).getTotal(), organizeTaskList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }
}
